package com.sean.findme.enjoy;

import java.util.ArrayList;
import java.util.HashMap;

import android.app.Activity;
import android.app.ActivityGroup;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;

import com.sean.findme.FmActivityMgrCtrller;
import com.sean.findme.FmDBHelp;
import com.sean.findme.R;
import com.sean.findme.dbentrys.EnjoyInfoItem;
import com.sean.findme.dbentrys.EnjoyInfos;
import com.sean.findme.dbentrys.XMLComm;
import com.sean.findme.near.NearActivity;

public class EnjoyHistoryActivity extends Activity {
	
	private ListView mListView;
	private ActivityGroup group;
	private EnjoyInfoAdapter adapter;

	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.enjoy_for_history);
		
		EnjoyInfos.getInstance().getHisList().clear();
		
		group = (ActivityGroup)getParent();
		
		ArrayList<XMLComm> list = FmDBHelp.getInstance(null, 0).commDBEntry.fetchAllRecordsByTable("history_tb");
		if( list != null )
		{
			for( int i = 0; i < list.size(); i++ )
			{
				String name = FmDBHelp.getInstance(null, 0).commDBEntry.getShopNameByKey(list.get(i).getUnknown1());
				
				EnjoyInfoItem item = new EnjoyInfoItem();
				item.setKey(list.get(i).getUnknown1());
				
				item.setName(name);
				
				EnjoyInfos.getInstance().getHisList().add(item);
			}
		}
		
		initViews();
	}

	private void initViews() {
		mListView = (ListView)findViewById(R.id.enjoy_history_list);
		adapter = new EnjoyInfoAdapter(EnjoyHistoryActivity.this, EnjoyInfos.getInstance().getHisList(), "history_tb");
		mListView.setAdapter(adapter);
		
		mListView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int index,
					long arg3) {
				HashMap<String, String> map = new HashMap<String, String>();
				map.put("title", getResources().getString(R.string.enjoy_type_history));
				map.put("key", EnjoyInfos.getInstance().getHisList().get(index).getKey());
				map.put("type", "1");
				
				FmActivityMgrCtrller.getInstance(null).getCurActivityMgr().JmpActivity(EnjoyHistoryActivity.this, NearActivity.class, group, map, true);
				
			}
		});
		
	}
}
